<template>
  <div v-if="!user.isLogin">
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: false, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: false, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
  <div v-else>
    <h1>欢迎用户：{{ user.username }}</h1>
    <van-button type="primary" @click="quit">退出登录</van-button>
  </div>
</template>


<script>
export default {
  data() {
    return {
      username: "",
      password: "",
      user: {
        isLogin: localStorage.getItem('token') ? true: false,
        username: localStorage.getItem('token')
      }
    };
  },
  methods: {
    // 登录函数
    onSubmit() {
      if (this.username === "admin" && this.password === "123456") {
        localStorage.setItem("token", this.username);
        this.$notify({ type: "success", message: "登录成功,跳转首页" });
        this.$router.push("/");
      } else {
        this.$notify({ type: "danger", message: "账户密码错误,请重新输入" });
      }
    },
    // 退出函数
    quit() {
      this.user = {
        isLogin: false,
        username: ''
      }

      localStorage.removeItem('token')
    }
  }
};
</script>